<?php
namespace app\designer\controller;

use app\admin\library\AdminController;
use cubox\Cubox;
use cubox\Cache;
use cubox\Data;

class Config extends AdminController{

    private function getApp(){
        $app = $this->post('app',$this->check('应用')->type('stirng')->required());
        if (!$this->app->design($app)){
            throw new \Exception('该应用不支持设计');
        }
        return $app;
    }

    public function onList(){
        $app = $this->getApp();
        $designer = $this->app->designer($app);
        $list =  $designer->configList??[];

        $re = [];
        foreach ($list as $key => $value) {
            $re[] = [
                'name' => $key,
                'title' => $value,
            ];
        }

        return $re;
    }


    public function onAddFile(){
        $app = $this->getApp();
        $filename = $this->post('filename',$this->check('文件名')->type('stirng')->required());
        $filecommment = $this->post('filecomment',$this->check('备注')->type('stirng')->required());
        $designer = $this->app->designer($app);
        
        $list =  $designer->configList??[];
        if (isset($list[$filename])){
            throw new \Exception('文件名已存在');
        }else{
            $list[$filename] = $filecommment;
            $designer->configList = $list;
            $designer->save();
            return true;
        }
    }
}